import { Controller } from 'egg';
export default class extends Controller {
  // 获取文章下的评论
  async getCommentsByArticleId() {
    const { ctx, service } = this;
    const articleId = Number(ctx.params.articleId);
    const result = await service.comment.list({
      articleId,
    });
    ctx.ok(result);
  }
  async save() {
    const { ctx, service } = this;
    const payLoad = ctx.request.body;
    payLoad.fromUserId = ctx.userId;
    await service.comment.doSave(payLoad);
    await service.article.updateCount(payLoad.articleId, 'commentCount', true);
    ctx.ok();
  }
  // 点赞与取消
  async toggleLike() {
    const { ctx, service } = this;
    const commentId = Number(ctx.params.commentId);
    const userId = ctx.userId;
    const result = await service.comment.toggleLike(commentId, userId);
    ctx.ok(result);
  }
  // 获取点赞用户
  async getLikeUsers() {
    const { ctx, service } = this;
    const commentId = Number(ctx.params.commentId);
    const result = await service.comment.getLikeUsersById(commentId);
    ctx.ok(result);
  }
}
